package demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyThreadLocal<T> {
    private Map<Thread, T> map = new HashMap<>();

    public void set(T value) {
        map.put(Thread.currentThread(), value);
    }

    public T get() {
        Thread thread = Thread.currentThread();
        T value = map.get(thread);
        if (value == null && !map.containsKey(thread)) {
            value = initialValue();
            map.put(thread, value);
        }
        return value;
    }

    public void remove() {
        map.remove(Thread.currentThread());
    }

    protected T initialValue() {
        return null;
    }

    public static void main(String[] args) {
        new MyThreadLocal<String>().set("world");
    }
}
